package org.spamforces.web.page;

import com.google.inject.Inject;
import org.spamforces.web.frame.Footer;
import org.spamforces.web.frame.Header;
import org.spamforces.web.frame.Menu;
import org.spamforces.web.frame.MessageBox;

/**
 * @author MikeMirzayanov (mirzayanovmr@gmail.com)
 */
public abstract class WebPage extends ApplicationPage {
    @Inject
    private MessageBox messageBox;

    @Inject
    private Header header;

    @Inject
    private Menu menu;

    @Inject
    private Footer footer;

    public abstract String getTitle();

    private long initializeActionStartTimeMillis;

    @Override
    public void initializeAction() {
        initializeActionStartTimeMillis = System.currentTimeMillis();

        super.initializeAction();

        if (isUserAuthorized()) {
            putGlobal("user", getUser());
        }

        if (!isAnonymousAccessAllowed() && !isUserAuthorized()) {
            setMessage($("Enter into the system"));
            abortWithRedirect("login");
        } else {
            putGlobal("pageTitle", getTitle());

            parse("messageBox", messageBox);
            parse("header", header);
            parse("menu", menu);
        }
    }

    @Override
    public void finalizeAction() {
        super.finalizeAction();

        putGlobal("executionPageTimeMillis", System.currentTimeMillis() - initializeActionStartTimeMillis);
        parse("footer", footer);
    }

    protected void setMessage(String message) {
        putSession("message", message);
    }
}
